package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Data time:2022/4/15 14:52
 * StudentID:2019112118
 * Author:hgw
 * Description: 支付宝沙箱测试配置类
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    //在支付宝创建的应用的id
    private   String app_id = "2021000119667766";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpzlIqpnBAEWCTIr4SaNrpQId9P2KTpJ7Pa2aGoElXYefW9BSlqv+oGz5hLn8VANNJAwjwazgDbIuaaTJ8yYYjWh1wxNf+c4iRDNpBb1qf9GZhrW4l/HGH6XCzFlRpo40CCCDBfgl+U7DLbI4h+4KXokEue6ALWsXBVTYLFTyxpsBC3UOauZUzccvHeczD746psV1oiYounxs4QWJrTBJtxWgRnD+mLPPtk79WjwYJAr0dYLACZVC6Bdi06khEgxnrbu3NJYH2qMts623cygQ0SWk2ZtRWhEwmpQ/Dd2ilsQKGQErkK5mVUmsuo87K4Z6Rq333B5Fleb4TLTVuWd+ZAgMBAAECggEAY/3a5MKd1xxkgkAzLSQRxMj7AAYTRl3qJrpX5W79wTcmDq4semH3qkZgtVlr/DJAOP5QhUKd+WYxzvujf1gsZSTrsTw49N2TzdaDr4SjGQ4SO/Kkqjm9oQsWEl9T1eE5Z7jhkQ9nB7zAnwmNqPUyMZiaSYUC+ay6Rt6mtGANHY7cFmLeBoz93W8V0ClhY2EwKMnyF/QlgR1no3qbjCMRsMHjkJoQBbswhoGpRSIbQidekB9cO71EFW/5dJV87W42UfoNQe4K61yhgWgrCHpVn9rFSdGIX0n8A48X/cPgYVu9Cm3GURoT71ePg1P++SwQGYnkO6xkR8PFMnpqpmDjAQKBgQDwkptaa+vWaFpGH6eSucYEZGMmdwyMiOfZo5kXgphMbd/nEyG0U0o2dtgWG1WbeS714PZgwgNnlidIOeNs0JDocamIAYSVgXdfZTPPJuyvUFVFbQGftPWGKSj90vWxHNS3oph6Yw41Del7UkqxFt1knDwycd4Amfo5nSEoeGSR0wKBgQC0sfZrPjn5hgSAkOazX+cugMfhYpZ69YLEVZkD9yVadV2D19xlwMSpf713g77ux2zSVKDIERbEgVkopjssGM64DgpkH7gwN18v+F6YcbCOB7gIX4hScykgVASXKTXyuUjGLaqs2aEuH/ULRxj+n8cH00x/RxGURROY52ciZRy5YwKBgH51cnh7loMkY5/M7/du9CpG4t/LYKtXJBkBqG31Vj2G3FXJdsQlrDMpEbm9MKkDcK4LTTfbhJKlGY0b8PK4SBQH+4fk1F8KqUdaGXvhCDW30rsl696Z7x5Q8J1MkZ5Ce4b0T5a2DzfQUlVjEqQ4UrSadAJIXNyQFDrI4C836hXFAoGAP0jo3gyQL3UhlImrUv1usVnHJ4fo3i2oW+0Cx2HCwljCpM9wUG7gMeEcUYRh1a0gztV27jsV90K6IEOAC+SwWcQJHaICV1i9TMa3ErsWs9e+O6iBzSaqK7lhVjPHwjfkZgxOb3VVPxtQLl/7QApjobj+XMFeRcifoXjCJUi2c7MCgYEAjpVdOAsZwrHI0YCuUU6xLHTagP9u80EMCr6D7xvfpimS1EL2wHxNwhZGzpTitFj25e8yTA27S2SYeHLvKBIaejclT1TY/Y9PSqmcvl2CCSB2hBw8gbxuBt607eObuR4fxt+/C11K9GAj6Dca+1pmoGfV0a0OPTfcESHQxXM89hg=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyay7fjd6Yf2pyGWQK/efrZggwUBE2fEvDoZ1+q6P21d86Rcbzz4lL2qrl4UPgChOE29SNRCKDnWIthXCBMTtRcf6SxCtaldp7D+1uXBmzxoEzKQ2lDjep+pnFMpoA+3CB4nOArL7B2hThh/F2ofbEDK0IJJVXWksZSSjfsKQm0+BXcrYMWe6khcf2S9NnBSnMB1bnHmnMK69oObsg8/dBp6cHruFoMu8OGCMIDO0Z6W7hoywzkf3K08VrqxGOhM5p94oGSBQJcD2CclK8c5wvHFMZm0wtmxWkyY2zdQ84stGJnLhX9ORmfHo/HBeXX8xPGF91SZU1yZ0gmatQZK91QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://mvaophzk6b.51xd.pub/payed/notify";


    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.cn/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 超时时间进行关单
    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
